using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualBasic.CompilerServices;
using System.Web;
using Microsoft.VisualBasic;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;
using System.IO;

namespace Adge.Common
{
    public class HtmlUtils
    {
        private static DateTime startTime = DateTime.Now;

        // Methods
        public static string Clean(string HTML, bool RemovePunctuation)
        {
            HTML = StripEntities(HTML, true);
            HTML = StripTags(HTML, true);
            if (RemovePunctuation)
            {
                HTML = StripPunctuation(HTML, true);
            }
            return HTML;
        }

        public static string FormatEmail(string Email)
        {
            string personalInfo = "";
            if (!string.IsNullOrEmpty(Email) && !string.IsNullOrEmpty(Email.Trim()))
            {
                if (Email.IndexOf("@") != -1)
                {
                    personalInfo = "<a href=\"mailto:" + Email + "\">" + Email + "</a>";
                }
                else
                {
                    personalInfo = Email;
                }
            }
            return CloakText(personalInfo);
        }

        public static string CloakText(string PersonalInfo)
        {
            if (PersonalInfo == null)
            {
                return null;
            }
            StringBuilder builder = new StringBuilder();
            builder.Remove(0, builder.Length);
            int num = PersonalInfo.Length - 1;
            int num3 = num;
            for (int i = 0; i <= num3; i++)
            {
                builder.Append(Strings.Asc(PersonalInfo.Substring(i, 1)).ToString());
                if (i < num)
                {
                    builder.Append(",");
                }
            }
            StringBuilder builder2 = new StringBuilder();
            builder2.Append("\r\n<script language=\"javascript\">\r\n");
            builder2.Append("<!-- \r\n");
            builder2.Append("   document.write(String.fromCharCode(" + builder.ToString() + "))\r\n");
            builder2.Append("// -->\r\n");
            builder2.Append("</script>\r\n");
            return builder2.ToString();
        }


        public static string FormatText(string HTML, bool RetainSpace)
        {
            string pattern = @"\s*<\s*[bB][rR]\s*/\s*>\s*";
            if (RetainSpace)
            {
            }
            return Regex.Replace(HTML, pattern, "\n");
        }

        public static string FormatWebsite(object Website)
        {
            if (Information.IsDBNull(RuntimeHelpers.GetObjectValue(Website)) || (Operators.CompareString(Strings.Trim(Website.ToString()), "", false) == 0))
            {
                return "";
            }
            if (Convert.ToBoolean(Strings.InStr(1, Website.ToString(), ".", CompareMethod.Binary)))
            {
                return ("<a href=\"" + Interaction.IIf(Convert.ToBoolean(Strings.InStr(1, Website.ToString(), "://", CompareMethod.Binary)), "", "http://").ToString() + Website.ToString() + "\">" + Website.ToString() + "</a>");
            }
            return Website.ToString();
        }

        public static string Shorten(string txt, int length, string suffix)
        {
            if (txt.Length > length)
            {
                return (txt.Substring(0, length) + suffix);
            }
            return txt;
        }

        public static string StripEntities(string HTML, bool RetainSpace)
        {
            string replacement;
            if (RetainSpace)
            {
                replacement = " ";
            }
            else
            {
                replacement = "";
            }
            return Regex.Replace(HTML, "&[^;]*;", replacement);
        }

        public static string StripNonWord(string HTML, bool RetainSpace)
        {
            string replacement;
            if (RetainSpace)
            {
                replacement = " ";
            }
            else
            {
                replacement = "";
            }
            if (HTML == null)
            {
                return HTML;
            }
            return Regex.Replace(HTML, @"\W*", replacement);
        }

        public static string StripPunctuation(string HTML, bool RetainSpace)
        {
            string replacement;
            string text = @"[~!#\$%\^&*\(\)-+=\{\[\}\]\|;:\x22'<,>\.\?\\\t\r\v\f\n]";
            Regex regex = new Regex(text + @"\s");
            Regex regex2 = new Regex(@"\s" + text);
            string input = HTML + " ";
            if (RetainSpace)
            {
                replacement = " ";
            }
            else
            {
                replacement = "";
            }
            while (regex2.IsMatch(input))
            {
                input = regex2.Replace(input, replacement);
            }
            while (regex.IsMatch(input))
            {
                input = regex.Replace(input, replacement);
            }
            return input;
        }

        public static string StripTags(string HTML, bool RetainSpace)
        {
            string replacement;
            if (RetainSpace)
            {
                replacement = " ";
            }
            else
            {
                replacement = "";
            }
            return Regex.Replace(HTML, "<[^>]*>", replacement);
        }

        public static string StripWhiteSpace(string HTML, bool RetainSpace)
        {
            string replacement;
            if (RetainSpace)
            {
                replacement = " ";
            }
            else
            {
                replacement = "";
            }
            return Regex.Replace(HTML, @"\s+", replacement);
        }


    }





}
